package com.sandking.db.jedis;

import java.util.ArrayList;
import com.sandking.db.dao.YhxzDao;
import com.sandking.db.bean.Yhxz;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import java.util.Set;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;

public class YhxzJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Yhxz getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhxz yhxz = null;
		try{
			String key = "Yhxz_Object";
			String field = SK_Plus.b("id:",id).e();
			yhxz = Yhxz.createForJson(jedis.hget(key,field));
			if(yhxz == null){
				yhxz = YhxzDao.getById(id);
				if(yhxz!=null){
					loadCache(yhxz);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhxz;
	}
	
	/**
	 * 根据( 用户_id  限制类型id ) 查询
	 */
	public static Yhxz getByYh_idXzlxid(int yh_id, int xzlxid){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhxz yhxz = null;
		try{
			String key = "Yhxz_Index";
			String field = SK_Plus.b("yh_id, xzlxid:",yh_id, xzlxid).e();
			String primaryKey = jedis.hget(key,field);
			if(primaryKey!=null){
				yhxz = getById(Integer.valueOf(primaryKey));
			}
			if(yhxz == null){
				yhxz = YhxzDao.getByYh_idXzlxid(yh_id, xzlxid);
				if(yhxz!=null){
					loadCache(yhxz);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhxz;
	}
	
	/**
	 * 根据( 用户_id ) 查询
	 */
	public static List<Yhxz> getByYh_id(int yh_id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Yhxz> yhxzs = new ArrayList<Yhxz>();
		try{
			String key = "Yhxz_Index";
			String field = SK_Plus.b(key,"yh_id:",yh_id).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Yhxz_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				yhxzs = Yhxz.createForJson(jsons);
			}else{
				yhxzs = YhxzDao.getByYh_id(yh_id);
				if(yhxzs!=null && !yhxzs.isEmpty()){
					loadCaches(yhxzs);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return yhxzs;
	}
	

	public static void loadCache(Yhxz yhxz){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(yhxz,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Yhxz yhxz,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhxz_Object";
		String field = SK_Plus.b("id:",yhxz.getId()).e();
		String data = yhxz.toJson();
		p.hset(key,field,data);
		
		// ---------------------- 唯一索引 ----------------------
		String primaryKey = String.valueOf(yhxz.getId());
		key = "Yhxz_Index";
		
		field = SK_Plus.b("yh_id, xzlxid:",yhxz.getYh_id(),yhxz.getXzlxid()).e();
		p.hset(key,field,primaryKey);
		
		// ---------------------- 聚集索引 ----------------------
		field = SK_Plus.b(key,"yh_id:",yhxz.getYh_id()).e();
		p.sadd(field, primaryKey);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Yhxz> yhxzs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhxz yhxz : yhxzs){
				loadCache(yhxz,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Yhxz yhxz){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(yhxz,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Yhxz yhxz,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhxz_Object";
		String field = SK_Plus.b("id:",yhxz.getId()).e();
		p.hdel(key,field);
		
		// ---------------------- 唯一索引 ----------------------
		key = "Yhxz_Index";
		
		field = SK_Plus.b("yh_id, xzlxid:",yhxz.getYh_id(),yhxz.getXzlxid()).e();
		p.hdel(key,field);
		
		// ---------------------- 聚集索引 ----------------------
		String primaryKey = String.valueOf(yhxz.getId());
		field = SK_Plus.b(key,"yh_id:",yhxz.getYh_id()).e();
		p.srem(field, primaryKey);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Yhxz> yhxzs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhxz yhxz : yhxzs){
				clearCache(yhxz,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Yhxz insert(Yhxz yhxz){
		yhxz = YhxzDao.insert(yhxz);
		if(yhxz!=null){
			loadCache(yhxz);
		}
    	return yhxz;
    }
    
    public static Yhxz update(Yhxz yhxz){
    	yhxz = YhxzDao.update(yhxz);
    	if(yhxz!=null){
			loadCache(yhxz);
		}
    	return yhxz;
    }
    
    public static boolean delete(Yhxz yhxz){
    	boolean bool = YhxzDao.delete(yhxz);
    	if(bool){
    		clearCache(yhxz);
    	}
    	return bool;
    }
}